# --*-- coding:utf-8 --*--
import requests
import json
import urllib
import warnings
warnings.filterwarnings("ignore")
from selenium import webdriver


class ZhiTongChe():

	''' 直通车类'''

	def __init__(self,cookie):
		super(ZhiTongChe, self).__init__()
		self.cookie = cookie
		# 获取token
		self.session = requests.session()
		self.headers = {
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
			'Host': 'subway.simba.taobao.com'
			}
		url = 'https://subway.simba.taobao.com/entry/login.htm'
		resp = self.session.get(url, verify=False,headers=self.headers,cookies = self.cookie)
		url = 'https://subway.simba.taobao.com/bpenv/getLoginUserInfo.htm'
		data = urllib.parse.urlencode({'_referer': '/home'})
		resp = self.session.post(url, headers=self.headers, data=data, verify=False, cookies = self.cookie)
		self.token = resp.json()['result']['token']

	def crowd_perspective(self):

		''' 直通车人群透视 '''

		url = 'https://subway.simba.taobao.com/report/getAreaPerspective.htm'
		queryString = {
			'bidwordstr':'口红',
			'startDate':'2018-11-01',
			'endDate':'2018-11-07'
		}

		headers = {
				'Host': 'subway.simba.taobao.com',
				'Connection': 'keep-alive',
				'Content-Length': '211',
				'Accept': 'application/json, text/javascript, */*; q=0.01',
				'Origin': 'https://subway.simba.taobao.com',
				'X-Requested-With': 'XMLHttpRequest',
				'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
				'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
				'Referer': 'https://subway.simba.taobao.com/',
				'Accept-Encoding': 'gzip, deflate, br',
				'Accept-Language': 'zh-CN,zh;q=0.9'

		}

		data = urllib.parse.urlencode({
			'sla':'json',
			'isAjaxRequest':'true',
			'token':self.token,
			'_referer':'/tools/insight/queryresult?kws=口红&tab=tabs-region',
			'_h':'196212396408204228200200424444220484464228444428',
			't':'1541643629636'
		})
		resp = self.session.post(url,params = queryString,data = data,cookies = self.cookie,verify=False,headers = headers)
		print (resp.url)
		print (resp.text)

	def test(self):

		'''	phantomjs 爬取数据 '''

		# driver = webdriver.PhantomJS()
		driver = webdriver.Firefox()

		driver.maximize_window()
		driver.get('https://www.taobao.com/')  # 地址栏里输入网址
		# driver.delete_all_cookies()
		for key, value in self.cookie.items():
			driver.add_cookie({"name":key,"value":value,"domain":'.taobao.com','expiry': 1573191196,'path': '/'})

		driver.get('https://subway.simba.taobao.com/#!/tools/insight/queryresult?kws=%E7%89%99%E8%86%8F')  # 地址栏里输入网址
		print (driver.page_source)



if __name__ == '__main__':
	
	cookies = {
        "uc1": "cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&tmb=1&cookie21=UtASsssmfuQi&cookie15=UIHiLt3xD8xYTw%3D%3D&existShop=true&pas=0&cookie14=UoTYN4fmRfq%2BTg%3D%3D&tag=8&lng=zh_CN",
        "tg": "0",
        "JSESSIONID": "6E738F5D42BD9E8456CB46B2EC2D3289",
        "tracknick": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "t": "088d600c416ce83b7b4d53748552a99e",
        "skt": "b751406f7494312e",
        "cna": "Y8NVFCbqyR4CAatx0yI1oeQ7",
        "csg": "2c575879",
        "_tb_token_": "5bd36b35abd8e",
        "_l_g_": "Ug%3D%3D",
        "sg": "%E5%BA%9702",
        "_cc_": "UIHiLt3xSw%3D%3D",
        "apushd2dce1c5dd89f161d480bff397a6184e": "%7B%22ts%22%3A1541655231938%2C%22parentId%22%3A1541655203778%7D",
        "mt": "np=",
        "cookie2": "1bb750c5ffc71273104d9611c63d2942",
        "cookie1": "UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D",
        "_nk_": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "v": "0",
        "isg": "BK6u2B5UeXp-QI2ewpLuXJcQ8wRwr3KpEWtDHdh3GrFsu04VQD_CuVQ5d6FXeGrB",
        "lgc": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "unb": "3397628220",
        "uc3": "vt3=F8dByR%2FIlR8gqQf4Fso%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=URm48syIIVrSKA%3D%3D",
        "dnk": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "cookie17": "UNN0nnQ5PzBj6g%3D%3D",
        "existShop": "MTU0MTY1NTE5Ng%3D%3D"
    }
	zhitongche = ZhiTongChe(cookies)
	zhitongche.test()
